package com.qcu.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itextpdf.text.DocumentException;
import com.qcu.entity.ExaminationInfo;
import com.qcu.service.ExaminationInfoService;
import com.qcu.mapper.ExaminationInfoMapper;
import com.qcu.util.PdfUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* @author MyComputer
* @description 针对表【examination_info】的数据库操作Service实现
* @createDate 2025-04-10 10:23:40
*/
@Service
public class ExaminationInfoServiceImpl extends ServiceImpl<ExaminationInfoMapper, ExaminationInfo>
    implements ExaminationInfoService{

    private static final String QUEUE_NAME = "send.mail";

    @Resource
    private ExaminationInfoMapper examinationInfoMapper;

    @Value("${spring.mail.username}")
    private String from;

    @Resource
    JavaMailSender javaMailSender;

    @Resource
    RedisTemplate redisTemplate;

    @Override
    public void sendResultToUser(String inhabitantId) {
        Map<String,String> map = new HashMap<>();
        map.put("inhabitantId",inhabitantId);

        redisTemplate.opsForStream().add(MapRecord.create(QUEUE_NAME,map));

//        try {
//            HashMap<String,String> data = examinationInfoMapper.sendResultToUser(inhabitantId);
//            String info = data.get("info");
//            JSONObject jsonObject = JSON.parseObject(info);
//            HashMap<String, String> resultMap = new HashMap<>();
//            if (jsonObject != null) {
//                for (String key : jsonObject.keySet()) {
//                    resultMap.put(key, jsonObject.getString(key));
//                }
//            }
//            byte[] bytes = PdfUtil.convertToPdf(resultMap);
//
//            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//            MimeMessageHelper mailMessage = new MimeMessageHelper(mimeMessage, true);
//
//            mailMessage.setFrom(from);
//            mailMessage.setTo(data.get("email"));
//            mailMessage.setSubject("体检结果");
//            mailMessage.setText("您预约的用户："+data.get("name")+" 的体检结果结果见附件");
//            mailMessage.addAttachment(data.get("name")+"-体检单.pdf",new ByteArrayDataSource(bytes,"application/pdf"));
//
//            javaMailSender.send(mimeMessage);
//
//        } catch (DocumentException e) {
//            throw new RuntimeException(e);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        } catch (MessagingException e) {
//            throw new RuntimeException(e);
//        }
    }
}




